#@weiXinLayout("收货地址管理")

#define weiXinMain()
<div class="nav-container">
    <div class="nav-item" onclick="window.location.href='#(contextPath)/weixin/mine'">
        <img class="icon-back" src="#(contextPath)/assets/front/img/icon/ic_keyboard_arrow_left_white_48dp.png">
    </div>
    <div class="nav-item nav-item-center">收货地址管理</div>
    <div class="nav-item" style="justify-content: space-around" onclick="window.location.href='#(contextPath)/weixin/receiver/addReceiver'">+添加</div>
</div>

<div class="weui-panel weui-panel_access" style="margin-top: 0px;">
    <div class="weui-panel__bd"  id="content" >
        #for(x:receiverList.list)
        <a href="javascript:void(0);" data-id="#(x.id??0)" id="id-#(x.id??0)" class="weui-media-box weui-media-box_appmsg">
            <div class="weui-media-box__bd" >
                <h4 class="weui-media-box__title">#(x.name??'') #if(x.is_default)[默认收货地址]#end</h4>
                <p class="weui-media-box__desc">#(x.mobile??'')</p>
                <p class="weui-media-box__desc">#(x.area_detail??'')</p>
            </div>
        </a>
        #else
        <div class='no-data'>暂无任何记录信息</div>
        #end
    </div>
</div>
#end
#define weiXinJs()
<script type="text/x-mustache" id="template">
    {{#items.list}}
        <a href="javascript:void(0);" data-id={{id}} id="id-{{id}}" class="weui-media-box weui-media-box_appmsg">
            <div class="weui-media-box__bd" >
                <h4 class="weui-media-box__title">{{name}} [默认收货地址]</h4>
                <p class="weui-media-box__desc">{{mobile}}</p>
                <p class="weui-media-box__desc">{{area_detail}}</p>
            </div>
        </a>
    {{/items.list}}
</script>

<script>
        function initData() {
            var config = {
                "url": "#(contextPath)/weixin/receiver/ajaxReceiverList",
                "data": {},
                "successCallBack": function (resp) {
                    var temp = $("#template").html();
                    if(resp.data.list.length>0){
                        var content = Mustache.render(temp, {items: resp.data});
                        $("#content").html(content);
                    }else{
                        $("#content").html("<div class='no-data'>暂无任何记录信息</div>");
                    }

                }
            }
            var ajax = new AjaxUtils(config);
            //ajax.executeRequest();
        }

        function markAsDefault(id) {
            var config = {
                "url": "#(contextPath)/weixin/receiver/ajaxMarkerReceiverAsDefault",
                "data": {id:id},
                "successCallBack": function (resp) {
                    if(resp.result){
                        $.alert(resp.data,function(){
                            window.location.reload();
                        })
                    }
                }
            }
            var ajax = new AjaxUtils(config);
            ajax.executeRequest();
        }

        function delReciver(id) {
            var self = this;
            var config = {
                "url": "#(contextPath)/weixin/receiver/ajaxDelReceiver",
                "data": {id:id},
                "successCallBack": function (resp) {
                    if(resp.result){
                        $.alert(resp.data,function(){
                            $("#id-"+id).remove();
                        })
                    }
                }
            }
            var ajax = new AjaxUtils(config);
            ajax.executeRequest();
        }


        $("#content").on("click",".weui-media-box",function(){

            var id = $(this).attr("data-id");

            var titleTip = "系统消息";
            var textTip = "确定要进行当前操作吗？";
            $.actions({
                actions: [{
                    text: "设为默认地址",
                    onClick: function() {
                        $.confirm({
                            title: titleTip,
                            text: textTip,
                            onOK: function () {
                                markAsDefault(id);
                            },
                            onCancel: function () {

                            }
                        });
                    }
                },{
                    text: "编辑",
                    onClick: function() {
                        window.location.href="#(contextPath)/weixin/receiver/editReceiver?id="+id;
                    }
                },{
                    text: "删除",
                    onClick: function() {
                        $.confirm({
                            title: titleTip,
                            text: textTip,
                            onOK: function () {
                                delReciver(id);
                            },
                            onCancel: function () {

                            }
                        });
                    }
                }]
            });
        });
    initData();
</script>
#end